package org.jgs1904.rabbitmq.messageacknowledgment;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import org.jgs1904.utils.RabbitMqUtils;

/**
 * @author: 默苍璃
 * @date: 2023-05-31 17:51
 * <p>
 * 工作线程3
 */
public class Worker03 {

    private static final String TASK_QUEUE_NAME = "UNFAIR";
    private static final String TASK_QUEUE_NAME1 = "ack_queue";

    public static void main(String[] args) throws Exception {

        Channel channel = RabbitMqUtils.getChannel();

        System.out.println("Worker03 等待消息:");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String receivedMessage = new String(delivery.getBody());
            System.out.println("Worker03 接收到消息:" + receivedMessage);

            /**
             * 1.消息标记 tag
             * 2.是否批量应答未应答消息
             */
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

        };

        CancelCallback cancelCallback = (consumerTag) -> {
            System.out.println(consumerTag + "消费者取消消费接口回调逻辑");
        };

        System.out.println("Worker03 消费者启动等待消费.................. ");

        //采用手动应答
        boolean autoAck = false;

        channel.basicConsume(TASK_QUEUE_NAME, autoAck, deliverCallback, cancelCallback);

    }

}
